package day05;

import java.awt.datatransfer.SystemFlavorMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

import day02.Title;

public class ClientContext {

	private Denglulei denglulei;
	private Caidanlei caidanlei;
	private Controller controller;
	private Kaoshilei kaoshilei;
	private Paper paper;
	private Title title;
	private long time=30;
	
	


	private int index=0;

	
	//设置第一个窗口关闭

	public void out() {
		denglulei.dispose();		
	}
	//登录按钮
	public void login() {
		String id =denglulei.getUseId();
		String password=denglulei.getPassword();
		User loginUser =controller.login(id, password);
		if(loginUser!=null) {
			caidanlei.updateView(loginUser.getUsername());
			caidanlei.setVisible(true);
			denglulei.dispose();
			kaoshilei.updateinformation("姓名："+loginUser.getUsername()+
					" "+"编号："+id+
					" "+"考试时间:30秒"+
					" "+"考试科目：老司机考试"+
					" "+"题目总数:20题");
		}
	}
//	public void questionfamiy() {
//		denglulei.j3.setText(paper.newList.get(1).toString());
//	}

	
	
	public void show() {
		denglulei.setVisible(true);
	}
	public void showtwo() {
		caidanlei.setVisible(true);
	}
	
	public void showthree() {
		kaoshilei.setVisible(true);
		String mm=((Title) paper.newList.get(index)).line();
		kaoshilei.updateamount(index+1);
		kaoshilei.updatequestion(mm);
		if(index==0) {
			kaoshilei.j5.setEnabled(false);
		}
		setTime();
//		indexcheck();

		
		
		
	}
	public Denglulei getDenglulei() {
		return denglulei;
	}

	public void setDenglulei(Denglulei denglulei) {
		this.denglulei = denglulei;
	}

	public Caidanlei getCaidanlei() {
		return caidanlei;
	}

	public void setCaidanlei(Caidanlei caidanlei) {
		this.caidanlei = caidanlei;
	}

	public void setController(Controller controller) {
		this.controller = controller;
	}
	public Paper getPaper() {
		return paper;
	}
	public void setPaper(Paper paper) {
		this.paper = paper;
	}
	public Kaoshilei getKaoshilei() {
		return kaoshilei;
	}
	public void setKaoshilei(Kaoshilei kaoshilei) {
		this.kaoshilei = kaoshilei;
	}
	public Controller getController() {
		return controller;
	}
	private Map<Integer,List> userAnwser=new HashMap<Integer,List>();
	private int allsorce=0;
	
	public void upquestion() {
		userAnwser.put(index, kaoshilei.getUserAnswers());
		//做一题查一题
//		allsorce-=5;
//		if(paper.getNewanwserlist().get(index).equals(kaoshilei.getUserAnswers())) {
//			allsorce+=5;
//			System.out.println(allsorce);
//		}
		
		String mm=((Title) paper.newList.get(--index)).line();
		System.out.println("上一题执行成功"+index);
		kaoshilei.j3.setText(mm);
		kaoshilei.updateamount(index+1);
//		System.out.println(kaoshilei.getUserAnswers());
		if(index==0) {
			kaoshilei.j5.setEnabled(false);
		}
		kaoshilei.j6.setEnabled(true);
		
	}

	public void downquestion() {
		kaoshilei.j5.setEnabled(true);
		userAnwser.put(index, kaoshilei.getUserAnswers());
		//做一题查一题
//		System.out.println(paper.getNewanwserlist().get(index));
//		System.out.println(kaoshilei.getUserAnswers());
//		allsorce+=5;
//		if(!paper.getNewanwserlist().get(index).equals(kaoshilei.getUserAnswers())) {
//			allsorce-=5;
//		System.out.println("匹配成功");
//			System.out.println(allsorce);
//		}

		String mm=((Title) paper.newList.get(++index)).line();
//		System.out.println("下一题执行成功"+index);
		kaoshilei.j3.setText(mm);
		kaoshilei.updateamount(index+1);
//		System.out.println(userAnwser);
		if(index==19) {
			kaoshilei.j6.setEnabled(false);
		}
		
	}
	public int getAllsorce() {
		return allsorce;
	}
	public void setAllsorce(int allsorce) {
		this.allsorce = allsorce;
	}
	public void downthree() {
		kaoshilei.setVisible(false);
		
	}
	public void setTime() {
		
		Thread tt=new Thread(new Runnable() {		
			public void run() {
				while(time>=0) {
					kaoshilei.j9.setText("剩余时间为:"+time+"秒");
					time--;
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						
						e.printStackTrace();
					}
				}
				showsorce();
			}			
		});
		tt.start();
	}
	//展示最终成绩
	public void showsorce() {
		userAnwser.put(index, kaoshilei.getUserAnswers());
		for(int i=0;i<userAnwser.size();i++) {
			System.out.println(userAnwser.get(i));
			System.out.println(paper.newanwserlist.get(i));
			if(paper.newanwserlist.get(i).equals(userAnwser.get(i))) {
				allsorce+=5;
			}
		}
		
		
		
//		if(paper.getNewanwserlist().get(index).equals(kaoshilei.getUserAnswers())) {
//			allsorce+=5;
//		System.out.println("匹配成功");
//			System.out.println(allsorce);
//		}	
		kaoshilei.j7.setEnabled(false);
		JOptionPane.showMessageDialog(null,"你的成绩为"+allsorce);
	}
	public void indexcheck() {
		Thread tt=new Thread(new Runnable() {

			public void run() {
				while(true) {
					if(index==0) {
						kaoshilei.j5.setEnabled(false);
					}
				}
			}
			
		});
		tt.start();
	}
	
	
    

	

	
}
